package m.jclecai.com.activity;

import android.content.Intent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import m.jclecai.com.R;
import m.jclecai.com.adapter.ExpandGroupBuyInfoAdapter;
import m.jclecai.com.bean.GroupBuyBean;
import m.jclecai.com.bean.GroupBuyDetailBean;
import m.jclecai.com.bean.RowsBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.OnItemClickListener;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.RecordBody;
import m.jclecai.com.view.CustomExpandListview;


/**
 * Created by ZD on 2017/5/15.
 * 追号记录
 */

public class ChaseRecordListActivity extends LotteryRecordActivity implements OnItemClickListener {

    @Override
    public int getContentView() {
        return R.layout.activity_chase_record_detail;
    }

    @Override
    public void initView() {
        super.initView();
        datas = new ArrayList<>();
        expand_list = (CustomExpandListview) findViewById(R.id.expand_list);
        expand_list.addHeaderView(View.inflate(this, R.layout.header_chase_detail, null));
        expand_list.addFooterView(View.inflate(this, R.layout.footer_group_buy_detail, null));
        //覆盖BaseActivity的引用 因为使用了 View.inflate 所以必须等到全部加载了在绑定 能够得到引用
        ButterKnife.bind(this);
        setTitle("追号详情");
        leftOptionText("");
        //总进球
        adapter = new ExpandGroupBuyInfoAdapter(this, expand_list, datas, this);
        expand_list.setAdapter(adapter);
        //悬浮的标题
        expand_list.setHeaderView(getLayoutInflater().inflate(
                R.layout.item_group_buy_parent, expand_list, false));
        expand_list.findViewById(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ChaseRecordListActivity.this, OrderDetailActivity.class)
                        .putExtra("id", groupBean.getId()));
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        String str = getIntent().getStringExtra("bean");
        groupBean = FastOk.get(str, GroupBuyBean.class);
        userBean = PreferenceUtil.readUser(this);
        if (ivIcon != null)
            ivIcon.setImageResource(getIcon());
        getData(Constant_Url.SELF_TRACE);
    }

    //获取参与用户
    @Override
    protected void getBuyUser() {
        showDialog();
        Map<String, String> pair = new HashMap<>();
        pair.put("id", detailBean.getTrace_classid());
        pair.put("page", String.valueOf(page));
        HttpManager.postAsyn(Constant_Url.SELF_TRACE_BUY_PEOPLE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                RecordBody bean = FastOk.get(response, RecordBody.class);
                if (bean != null && 0 == bean.getStatus()) {
                    if (datas.size() >= 2)
                        datas.get(1).addAll(bean.getRecords());
                    else
                        datas.add(bean.getRecords());
                    adapter.isShowMore(bean.getPages() != page);
                    page++;
                }
                adapter.notifyDataSetChanged();
            }
        }, pair);
    }

    @Override
    protected void updateText(GroupBuyDetailBean bean) {
        //截止时间
//        BaseUtils.saleEnd(tvHeadEndTime, DateUtil.strToLong(bean.getEndtime()), 1000);
        if (tvHeadEndTime != null)
            tvHeadEndTime.setText((bean.getTotal_winmoney() + "元"));
        //订单金额
        if (tvOrderMoney != null)
            tvOrderMoney.setText(bean.getTrace_money());
        //停止条件
        if (tvStop != null)
            tvStop.setText("中奖后停止");
        //投注金额
        if (tvMoney != null)
            tvMoney.setText(bean.getOne_money());
        //彩种
        if (tvTitle != null)
            tvTitle.setText(bean.getType());
        //期号
        if (tvNum != null)
            tvNum.setText("第" + bean.getTermNo() + "期");
        //订单编号
        if (tvOrder != null)
            tvOrder.setText(bean.getProjectno());
        //方案宣言
        if (tvDesc != null)
            tvDesc.setText(bean.getTeambuy_content());
        //下单时间
        if (tvTime != null)
            tvTime.setText(bean.getAddtime());
        //截止时间
        if (tvFooterEndtime != null)
            tvFooterEndtime.setText(bean.getEndtime());
        //怎么领奖
        if (tvGetGift != null)
            tvGetGift.setText("如中奖，彩金直接返还至购彩账户");
    }

    @Override
    public void onItemClick(int position) {
        if(position == -1){
            //表示加载更多
            getBuyUser();
            return;
        }
        //datas 下标1的数据类型为List<NewsBean>
        List<RowsBean> records = datas.get(1);
        detailBean.setPlanNo(records.get(position).getProjectno());
        detailBean.setTermNo(records.get(position).getExpect());
        detailBean.setOpencodes(records.get(position).getOpencodes());
        detailBean.setResult(records.get(position).getIstate());
        detailBean.setEndtime(records.get(position).getEndtime());
//        GroupBuyBean bean = new GroupBuyBean();
//        bean.setId(records.get(position).getId());
//        bean.setLotteryid(records.get(position).getLotteryid());
//        bean.setProjectno(records.get(position).getProjectno());
        startActivity(new Intent(this, BuyDetailActivity.class)
                .putExtra("detailBean", JSON.toJSONString(detailBean)));
    }
}
